<template>
  <div class="overview-right">
    <div class="history-overview">
      <div class="title">
        <div class="title-des">历史总览（电站）</div>
      </div>
      <div class="ul">
        <div class="li">
          <div class="li-num">
            {{ formatNum(data.historyData?.totalPower, 4).value
            }}<span
              v-if="formatNum(data.historyData?.totalPower, 4).state"
              class="li-unit"
              >万</span
            >
          </div>
          <div class="li-des">累计充电度数（度）</div>
        </div>
        <div class="li">
          <div class="li-num">
            {{ formatNum(data.historyData?.actualAmount, 2).value
            }}<span
              v-if="formatNum(data.historyData?.actualAmount, 2).state"
              class="li-unit"
              >万</span
            >
          </div>
          <div class="li-des">累计充电金额（元）</div>
        </div>
        <div class="li">
          <div class="li-num">
            {{ formatNum(data.historyData?.count, 0).value
            }}<span
              v-if="formatNum(data.historyData?.count, 0).state"
              class="li-unit"
              >万</span
            >
          </div>
          <div class="li-des">累计充电次数（次）</div>
        </div>
        <div class="li">
          <div class="li-num">
            {{ formatNum(data.historyData?.chargedTime, 2).value
            }}<span
              v-if="formatNum(data.historyData?.chargedTime, 2).state"
              class="li-unit"
              >万</span
            >
          </div>
          <div class="li-des">累计充电时长（小时）</div>
        </div>
      </div>
    </div>
    <div class="terminal">
      <div class="title">
        <div class="title-des">充电终端监控</div>
      </div>
      <div class="ul" :style="envType === 'lznt' ? { padding: '0 5px' } : ''">
        <div class="li">
          <div
            class="li-dot"
            v-if="data.terminalData?.chargePercentage !== '-1'"
          >
            <div
              v-for="(item, index) in chargePercentComputed"
              :key="index"
              class="li-dot-li"
              :style="`transform: rotateZ(${
                180 + Number(index) * 6
              }deg) translateY(48px)`"
            ></div>
          </div>
          <div
            class="li-num"
            v-if="data.terminalData?.chargePercentage === '-1'"
          >
            --
          </div>
          <div class="li-num" v-else>
            {{ ((data.terminalData?.chargePercentage || 0) * 100).toFixed(2) }}%
          </div>
          <div class="li-des">充电</div>
        </div>
        <div class="li">
          <div class="li-dot" v-if="data.terminalData?.freePercentage !== '-1'">
            <div
              v-for="(item, index) in freePercentComputed"
              :key="index"
              class="li-dot-li"
              :style="`transform: rotateZ(${
                180 + Number(index) * 6
              }deg) translateY(48px)`"
            ></div>
          </div>
          <div class="li-num" v-if="data.terminalData?.freePercentage === '-1'">
            --
          </div>
          <div class="li-num" v-else>
            {{ ((data.terminalData?.freePercentage || 0) * 100).toFixed(2) }}%
          </div>
          <div class="li-des">空闲</div>
        </div>
        <template v-if="envType === 'lznt'">
          <div class="li">
            <div
              class="li-dot"
              v-if="data.terminalData?.offLinePercentage !== '-1'"
            >
              <div
                v-for="(item, index) in offLinePercentageComputed"
                :key="index"
                class="li-dot-li"
                :style="`transform: rotateZ(${
                  180 + Number(index) * 6
                }deg) translateY(48px)`"
              ></div>
            </div>
            <div
              class="li-num"
              v-if="data.terminalData?.offLinePercentage === '-1'"
            >
              --
            </div>
            <div class="li-num" v-else>
              {{
                ((data.terminalData?.offLinePercentage || 0) * 100).toFixed(2)
              }}%
            </div>
            <div class="li-des">离线</div>
          </div>
          <div class="li">
            <div
              class="li-dot"
              v-if="data.terminalData?.failPercentage !== '-1'"
            >
              <div
                v-for="(item, index) in failPercentageComputed"
                :key="index"
                class="li-dot-li"
                :style="`transform: rotateZ(${
                  180 + Number(index) * 6
                }deg) translateY(48px)`"
              ></div>
            </div>
            <div
              class="li-num"
              v-if="data.terminalData?.failPercentage === '-1'"
            >
              --
            </div>
            <div class="li-num" v-else>
              {{ ((data.terminalData?.failPercentage || 0) * 100).toFixed(2) }}%
            </div>
            <div class="li-des">故障</div>
          </div>
        </template>
        <template v-else>
          <div class="li">
            <div
              class="li-dot"
              v-if="data.terminalData?.abnormalPercentage !== '-1'"
            >
              <div
                v-for="(item, index) in abnormalPercentComputed"
                :key="index"
                class="li-dot-li"
                :style="`transform: rotateZ(${
                  180 + Number(index) * 6
                }deg) translateY(48px)`"
              ></div>
            </div>
            <div
              class="li-num"
              v-if="data.terminalData?.abnormalPercentage === '-1'"
            >
              --
            </div>
            <div class="li-num" v-else>
              {{
                ((data.terminalData?.abnormalPercentage || 0) * 100).toFixed(2)
              }}%
            </div>
            <div class="li-des">待启用</div>
          </div></template
        >
      </div>
    </div>
    <div class="realtime-charging">
      <div class="title">
        <div class="title-des">当日实时充电分布（{{ nowTime }}）</div>
      </div>
      <div class="ul">
        <chart-realtime :data="data.realtimeData"></chart-realtime>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import dayjs from 'dayjs';
import ChartRealtime from './chartRealtime.vue';

interface Props {
  data: any;
  time: string;
}
const prop = withDefaults(defineProps<Props>(), {
  data: () => {},
  time: dayjs().format('YYYY-MM-DD'),
});
const nowTime = ref(dayjs().format('YYYY-MM-DD'));

const envType = import.meta.env.VITE_ENV_TYPE;

const chargePercentComputed = computed(() => {
  // 60
  if (
    prop.data.terminalData &&
    prop.data.terminalData?.chargePercentage !== '-1'
  ) {
    if (isNaN(Number(prop.data.terminalData?.chargePercentage))) {
      return [];
    }
    return Math.round(Number(prop.data.terminalData?.chargePercentage) * 60);
  }
  return [];
});

const freePercentComputed = computed(() => {
  // 60
  if (
    prop.data.terminalData &&
    prop.data.terminalData?.freePercentage !== '-1'
  ) {
    if (isNaN(Number(prop.data.terminalData?.freePercentage))) {
      return [];
    }
    return Math.round(Number(prop.data.terminalData?.freePercentage) * 60);
  }
  return [];
});

const abnormalPercentComputed = computed(() => {
  // 60
  if (
    prop.data.terminalData &&
    prop.data.terminalData?.abnormalPercentage !== '-1'
  ) {
    if (isNaN(Number(prop.data.terminalData?.abnormalPercentage))) {
      return [];
    }
    return Math.round(Number(prop.data.terminalData?.abnormalPercentage) * 60);
  }
  return [];
});

const offLinePercentageComputed = computed(() => {
  // 60
  if (
    prop.data.terminalData &&
    prop.data.terminalData?.offLinePercentage !== '-1'
  ) {
    if (isNaN(Number(prop.data.terminalData?.offLinePercentage))) {
      return [];
    }
    return Math.round(Number(prop.data.terminalData?.offLinePercentage) * 60);
  }
  return [];
});

const failPercentageComputed = computed(() => {
  // 60
  if (
    prop.data.terminalData &&
    prop.data.terminalData?.failPercentage !== '-1'
  ) {
    if (isNaN(Number(prop.data.terminalData?.failPercentage))) {
      return [];
    }
    return Math.round(Number(prop.data.terminalData?.failPercentage) * 60);
  }
  return [];
});

watch(
  () => prop.time,
  (newVal, oldVal) => {
    nowTime.value = dayjs(newVal).format('YYYY-MM-DD');
  }
);

const formatNum = (val: string | number, length: number = 3) => {
  if (isNaN(Number(val))) {
    return {
      value: '--',
      state: false,
    };
  }
  if (Number(val) > 10000) {
    return {
      value: `${parseFloat((Number(val) / 10000).toFixed(2))}`,
      state: true,
    };
  }

  return {
    value: Number(val).toFixed(length) || '--',
    state: false,
  };
};
</script>
<style lang="scss" scoped>
.overview-right {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding-right: 23px;
  position: relative;
  z-index: 2;
  .title {
    width: 467px;
    height: 30px;
    background-image: url('@/assets/images/overView/title-bg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    margin-bottom: 3px;
    &-des {
      padding-top: 11px;
      padding-left: 32px;
      font-size: 12px;
      color: #dfeef3;
    }
  }
  .history-overview {
    .ul {
      box-sizing: border-box;
      width: 614px;
      height: 332px;
      border-radius: 5px;
      border: 1px solid rgba(48, 172, 243, 0.9);
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
      align-items: center;
      padding: 11px 0;
      .li {
        width: 213px;
        height: 116px;
        background-image: url('@/assets/images/overView/icon3.png');
        background-size: 100% 100%;
        background-repeat: no-repeat;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        &-num {
          font-size: 36px;
          font-weight: bold;
          color: #50dbff;
        }
        &-des {
          margin-top: 5px;
          width: 125px;
          height: 23px;
          background: linear-gradient(90deg, #0066e5, #00b6ff);
          border-radius: 4px 4px 4px 4px;
          font-size: 12px;
          color: #ffffff;
          text-align: center;
          line-height: 23px;
        }
        &-unit {
          font-weight: normal;
          font-size: 10px;
        }
      }
    }
  }
  .terminal {
    .ul {
      box-sizing: border-box;
      width: 614px;
      height: 236px;
      border-radius: 5px;
      border: 1px solid rgba(48, 172, 243, 0.9);
      display: flex;
      justify-content: space-around;
      align-items: center;
      padding: 0 20px;
      .li {
        width: 124px;
        height: 124px;
        background-image: url('@/assets/images/overView/terminal-bg-1.png');
        background-size: 100% 100%;
        background-repeat: no-repeat;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        position: relative;
        &-num {
          font-size: 22px;
          color: #ffffff;
        }
        &-des {
          font-size: 12px;
          color: #e1e1e1;
        }
        &-dot {
          &-li {
            position: absolute;
            top: 62px;
            width: 2.5px;
            height: 5px;
            background-color: #44cff9;
          }
        }
      }
    }
  }
  .realtime-charging {
    .ul {
      box-sizing: border-box;
      width: 614px;
      height: 256px;
      border-radius: 5px;
      border: 1px solid rgba(48, 172, 243, 0.9);
    }
  }
}
</style>